﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using RpgLibrary.ItemClasses;
using RpgLibrary;

namespace RpgEditor
{
    public partial class FormArmorDetails : FormDetailBase, IDetailForm
    {
        #region Field Region

        ArmorData armor = null;
        #endregion
        
        #region Property Region
        
        public ArmorData Armor
        {
            get { return armor; }
            set { armor = value; }
        }

        public override IRpgDataObject DataObject
        {
            get
            {
                return (IRpgDataObject)armor;
            }
            set
            {
                armor = (ArmorData)value;
            }
        }

        #endregion
        
        #region Constructor Region
        
        public FormArmorDetails()
        {
            InitializeComponent();

            InitializeFormLists();

            btnAddAllowed.Click += new EventHandler(btnAddAllowed_Click);
            btnRemoveAllowed.Click += new EventHandler(btnRemoveAllowed_Click);
            btnOK.Click += new EventHandler(btnOK_Click);
            btnCancel.Click += new EventHandler(btnCancel_Click);

            SetDataObjectValueFunction = SetArmorValue;
            LoadDataObjectValueFunction = LoadArmorValue;

        }

        private void InitializeFormLists()
        {
            foreach (string entityName in FormMain.EntityDataManager.CharacterData.Keys)
                lbAvailableClasses.Items.Add(entityName);

            foreach (ArmorLocation location in Enum.GetValues(typeof(ArmorLocation)))
                cbArmorLocation.Items.Add(location);

            cbArmorLocation.SelectedIndex = 0;
        }

        #endregion

        #region Event Handler Region

        void btnAddAllowed_Click(object sender, EventArgs e)
        {
            while (lbAvailableClasses.SelectedItems.Count > 0)
            {
                lbAllowedClasses.Items.Add(lbAvailableClasses.SelectedItems[0]);
                lbAvailableClasses.Items.Remove(lbAvailableClasses.SelectedItems[0]);
            }
        }

        void btnRemoveAllowed_Click(object sender, EventArgs e)
        {
            while (lbAllowedClasses.SelectedItems.Count > 0)
            {
                lbAvailableClasses.Items.Add(lbAllowedClasses.SelectedItems[0]);
                lbAllowedClasses.Items.Remove(lbAllowedClasses.SelectedItems[0]);
            }
        }

   

        void LoadArmorValue()
        {
            if (armor != null)
            {
                tbName.Text = armor.Name;
                tbType.Text = armor.Type;
                nudPrice.Value = armor.Price;
                nudWeight.Value = (decimal)armor.Weight;
                cbArmorLocation.SelectedIndex = (int)armor.ArmorLocation;
                nudDefenseValue.Value = armor.DefenseValue;
                nudDefenseModifier.Value = armor.DefenseModifier;
                
                foreach (string className in armor.AllowableClasses)
                {
                    if (lbAvailableClasses.Items.Contains(className))
                        lbAvailableClasses.Items.Remove(className);
                    
                    lbAllowedClasses.Items.Add(className);
                }
            }
        }
        
        bool SetArmorValue()
        {
            if (string.IsNullOrEmpty(tbName.Text))
            {
                MessageBox.Show("You must enter a name for the item.");
                return false;
            }
            
            List<string> allowedClasses = new List<string>();
            foreach (object o in lbAllowedClasses.Items)
                allowedClasses.Add(o.ToString());
            
            armor = new ArmorData();
            armor.Name = tbName.Text;
            armor.Type = tbType.Text;
            armor.Price = (int)nudPrice.Value;
            armor.Weight = (float)nudWeight.Value;
            armor.ArmorLocation = (ArmorLocation)cbArmorLocation.SelectedIndex;
            armor.DefenseValue = (int)nudDefenseValue.Value;
            armor.DefenseModifier = (int)nudDefenseModifier.Value;
            armor.AllowableClasses = allowedClasses.ToArray();

            return true;
        }
        
        #endregion
    }
}